package com.zhipu.balance.ui.vm.home.calculate.soybean

import android.text.TextUtils
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.LogUtils
import com.zhipu.balance.mobile.event.Personnel
import com.zhipu.balance.mobile.event.RecordSoybean
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.vm.BaseViewModel
import com.zhipu.balance.utils.CommonUtils

/**
 * 大豆损伤
 */
class SoybeanDamageViewModel(mSystemApiService : SystemApiService) : BaseViewModel(mSystemApiService){


    /**
     * 重量
     */
    var weighing : String? = null

    /**
     * 大豆实体
     */
    var mRecordSoybean : RecordSoybean? = null


    /**
     * 当前的检查人员
     */
    var mPersonnel : Personnel? = null


    /**
     * 损伤粒(含热损伤粒)质量
     */
    var damagedGrainQuality = ObservableField("")

    /**
     * 大样质量
     */
    var largeMiscellaneousSampleQuality = ObservableField("")

    /**
     * 大样杂质
     */
    var detailImpurity = ObservableField("")

    /**
     * 小样质量
     */
    var smallMiscellaneousSampleQuality = ObservableField("")


    /**
     * 损伤粒率
     */
    var damagedGrainResult = ObservableField("")




    /**
     * 更新页面
     */
    fun updateRecordRice(){
        if(!TextUtils.isEmpty(mRecordSoybean?.damagedGrainRecorder)) {
            //损伤粒(含热损伤粒)质量
            damagedGrainQuality.set(mRecordSoybean?.damagedGrainQuality.toString())
            //大样质量
            largeMiscellaneousSampleQuality.set(mRecordSoybean!!.largeMiscellaneousSampleQuality.toString())
            //大样杂质
            detailImpurity.set(mRecordSoybean!!.detailImpurity.toString())
            //小样质量
            smallMiscellaneousSampleQuality.set(mRecordSoybean!!.smallMiscellaneousSampleQuality.toString())
            //损伤粒率
            damagedGrainResult.set(mRecordSoybean?.damagedGrainResult.toString())
        }
    }


    /**
     * 释放更新页面
     */
    fun releaseRecordRice(){
        //损伤粒(含热损伤粒)质量
        damagedGrainQuality.set("")
        //大样质量
        largeMiscellaneousSampleQuality.set("")
        //大样杂质
        detailImpurity.set("")
        //小样质量
        smallMiscellaneousSampleQuality.set("")
        //损伤粒率
        damagedGrainResult.set("")
    }




    /**
     * 损伤粒(含热损伤粒)质量
     */
    fun BulkQuality(){
        damagedGrainQuality.set(weighing)
        mRecordSoybean?.damagedGrainQuality = damagedGrainQuality.get()?.toDouble()
    }



    /**
     * 刷新数据
     */
    fun refreshData(){
        //大样质量
        largeMiscellaneousSampleQuality.set(mRecordSoybean!!.largeMiscellaneousSampleQuality.toString())
        //大样杂质
        detailImpurity.set(mRecordSoybean!!.detailImpurity.toString())
        //小样质量
        smallMiscellaneousSampleQuality.set(mRecordSoybean!!.smallMiscellaneousSampleQuality.toString())

    }



    /**
     * 计算
     */
    fun calculation(){
        //大样
        val BigPicture: Double = mRecordSoybean!!.detailImpurity / mRecordSoybean!!.largeMiscellaneousSampleQuality
        LogUtils.d("大样$BigPicture")
        //小样
        val sample: Double = mRecordSoybean!!.damagedGrainQuality / mRecordSoybean!!.smallMiscellaneousSampleQuality
        LogUtils.d("小样$sample")
        //损伤粒率
        val DamagedGrainRate: Double = CommonUtils.formatDouble((1 - BigPicture) * sample * 100)
        mRecordSoybean!!.damagedGrainResult = DamagedGrainRate
        damagedGrainResult.set(DamagedGrainRate.toString())



        /**
         * 损伤粒记录员
         */
        mRecordSoybean!!.damagedGrainRecorder = mPersonnel!!.name


        //损伤粒(含热损伤粒)质量
        if(mRecordSoybean?.damagedGrainQuality == 0.00){
            damagedGrainQuality.set("0.00")
        }

    }


}